package com.frogs.ucenter.service.daoimpl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.base.dao.BaseDao;
import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.framework.exception.BusinessException;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.util.ExpandCodeEnum;
import com.frogs.system.util.UserIntegralRecordEnum;
import com.frogs.ucenter.dao.UserIntegralAdminUpdateDao;
import com.frogs.ucenter.dao.UserIntegralBalanceDao;
import com.frogs.ucenter.dao.UserIntegralRecordDao;
import com.frogs.ucenter.entity.UserIntegralAdminUpdate;
import com.frogs.ucenter.entity.UserIntegralBalance;
import com.frogs.ucenter.entity.UserIntegralRecord;
import com.frogs.ucenter.service.UserIntegralBalanceService;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("userIntegralBalanceService")
public class UserIntegralBalanceServiceImpl extends BaseServiceImpl<UserIntegralBalance> implements UserIntegralBalanceService {
    
	@Resource
    private UserIntegralBalanceDao userIntegralBalanceDao;

	@Resource
    private UserIntegralRecordDao userIntegralRecordDao;
	
	@Resource
    private UserIntegralAdminUpdateDao userIntegralAdminUpdateDao;
	
	
    
    public UserIntegralBalanceServiceImpl() {
        super();
    }

    public UserIntegralBalanceDao getUserIntegralBalanceDao() {
        return userIntegralBalanceDao;
    }

    /**
     * Don't remove or edit it.
     */
    @Override
    protected BaseDao<UserIntegralBalance> getBaseDao() {
        return userIntegralBalanceDao;
    }

    public void setUserIntegralBalanceDao(UserIntegralBalanceDao userIntegralBalanceDao) {
        this.userIntegralBalanceDao = userIntegralBalanceDao;
    }

    @Override
    public int update(UserIntegralBalance userIntegralBalance , Integer oldBalance,String updateReason,Integer updateId) throws BusinessException{
    	/*if(userIntegralBalance.getIntegralUsable() < oldBalance)
    		throw new BusinessException("new balance too small","设置积分不能比原积分小"); 
    	*/
    	if(userIntegralBalance.getIntegralUsable() == oldBalance)
    		throw new BusinessException("balance no charge","设置积分等于原积分，不予修改"); 
    	
    	UserIntegralRecord record = new UserIntegralRecord();
    	record.setIntegralNum(userIntegralBalance.getIntegralUsable() - oldBalance);
    	record.setType(UserIntegralRecordEnum.ADMIN_DONATE.getId());
    	record.setUserId(userIntegralBalance.getId());
    	record.setRemark(updateReason);
    	record.setApplicablePlatformId(ApplicablePlatformEnum.ALL.getId());
    	record.setExpandCode(ExpandCodeEnum.ALL.getId());
    	userIntegralRecordDao.insert(record);
    	
    	UserIntegralAdminUpdate adminUpdate = new UserIntegralAdminUpdate();
    	adminUpdate.setUpdateNum(userIntegralBalance.getIntegralUsable() - oldBalance);
    	adminUpdate.setUpdateReason(updateReason);
    	adminUpdate.setUserId(userIntegralBalance.getId());
    	adminUpdate.setUpdateId(updateId);
    	userIntegralAdminUpdateDao.insert(adminUpdate);
        return super.update(userIntegralBalance);
    }

	@Override
	public void add(Integer balance, String updateReason, Integer updateId, Integer userId)throws BusinessException {
    	UserIntegralRecord record = new UserIntegralRecord();
    	record.setIntegralNum(balance);
    	record.setType(UserIntegralRecordEnum.ADMIN_DONATE.getId());
    	record.setUserId(userId);
    	record.setRemark(updateReason);
    	record.setApplicablePlatformId(ApplicablePlatformEnum.ALL.getId());
    	record.setExpandCode(ExpandCodeEnum.ALL.getId());
    	userIntegralRecordDao.insert(record);
    	
    	UserIntegralAdminUpdate adminUpdate = new UserIntegralAdminUpdate();
    	adminUpdate.setUpdateNum(balance);
    	adminUpdate.setUpdateReason(updateReason);
    	adminUpdate.setUserId(userId);
    	adminUpdate.setUpdateId(updateId);
    	userIntegralAdminUpdateDao.insert(adminUpdate);
    	
    	UserIntegralBalance userIntegralBalance = new UserIntegralBalance();
    	userIntegralBalance.setId(userId);
    	userIntegralBalance.setIntegralUsable(balance);
    	userIntegralBalance.setIntegralUsed(0);
    	
        super.insert(userIntegralBalance);
	}
}